package com.haircut.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haircut.base.Result;
import com.haircut.enums.RechargeStatus;
import com.haircut.exception.BusinessException;
import com.haircut.mapper.HaircutUserOrder;
import com.haircut.mapper.HaircutUserRecharge;
import com.haircut.mapper.WechatUser;
import com.haircut.model.dto.UserRechargeDTO;
import com.haircut.model.req.AdminBuyReq;
import com.haircut.model.req.AdminRechargeReq;
import com.haircut.model.req.RechargeReq;
import com.haircut.service.HaircutUserRechargeService;
import com.haircut.mapper.HaircutUserRechargeMapper;
import com.haircut.service.WechatUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author Administrator
* @description 针对表【t_haircut_user_recharge(用户充值记录表)】的数据库操作Service实现
* @createDate 2025-11-01 22:57:36
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class HaircutUserRechargeServiceImpl extends ServiceImpl<HaircutUserRechargeMapper, HaircutUserRecharge>
    implements HaircutUserRechargeService{

    private final WechatUserService wechatUserService;

    @Override
    public List<HaircutUserRecharge> getRecharge(Long userId,String phone) {
        LambdaUpdateWrapper<HaircutUserRecharge> query = new LambdaUpdateWrapper<HaircutUserRecharge>()
                .eq(HaircutUserRecharge::getStatus, RechargeStatus.SUCCESS.getCode());
        if(ObjectUtil.isNotNull(userId)){
            query.eq(HaircutUserRecharge::getUserId, userId);
        }
        if(ObjectUtil.isNotNull(phone)){
            query.eq(HaircutUserRecharge::getPhone, phone);
        }
        query.orderByDesc(HaircutUserRecharge::getCreateTime);
        List<HaircutUserRecharge> list = baseMapper.selectList(query);
        return list;
    }
    @Override
    public Page<UserRechargeDTO> getRechargeAll(RechargeReq req) {
        LambdaQueryWrapper<HaircutUserRecharge> queryWrapper = new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotEmpty(req.getPhone())){
            queryWrapper.eq(HaircutUserRecharge::getPhone, req.getPhone());
        }
        queryWrapper.orderByDesc(HaircutUserRecharge::getCreateTime);
        Page<HaircutUserRecharge> page = new Page<>(req.getPageNum(), req.getPageSize());
        Page<HaircutUserRecharge> result = baseMapper.selectPage(page,queryWrapper);
        List<HaircutUserRecharge> records = result.getRecords();
        List<UserRechargeDTO> userRechargeDTOS = BeanUtil.copyToList(records, UserRechargeDTO.class);
        Page<UserRechargeDTO> userRechargeDTOPage = new Page<>();
        userRechargeDTOPage.setTotal(result.getTotal());
        userRechargeDTOPage.setRecords(userRechargeDTOS);
        return userRechargeDTOPage;
    }
    @Override
    public void adminRecharge(AdminRechargeReq rechargeReq) {
        WechatUser wechatUser = wechatUserService.getByPhone(rechargeReq.getPhone());
        if(ObjectUtil.isNull(wechatUser)){
            Result<String> result = wechatUserService.register(rechargeReq.getPhone());
            if(result.getCode() == 0){
                //注册成功
                log.info("用户充值注册成功","");
                wechatUser = wechatUserService.getByPhone(rechargeReq.getPhone());
            }else{
                throw new BusinessException("该用户没有注册过小程序");
            }
        }
        HaircutUserRecharge recharge = new HaircutUserRecharge();
        recharge.setUserId(wechatUser.getId());
        recharge.setPhone(rechargeReq.getPhone());
        recharge.setAmount(rechargeReq.getAmount());
        recharge.setCurrency("CNY");
        recharge.setPayType(1);
        recharge.setOrderNo(IdUtil.getSnowflake().nextIdStr());
        recharge.setStatus(RechargeStatus.SUCCESS.getCode());
        recharge.setRemark("后台手动充值");
        recharge.setPayTime(new Date());
        baseMapper.insert(recharge);
        WechatUser wechatUserUpdate = new WechatUser();
        wechatUserUpdate.setId(wechatUser.getId());
        wechatUserUpdate.setBalance(wechatUser.getBalance().add(rechargeReq.getAmount()));
        wechatUserService.updateById(wechatUserUpdate);
    }

}




